/*   Copyright (C) 2008  Jeroen v. Schagen & Youri Lima
 *
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

using System;
using System.Collections;
using System.Windows.Forms;

using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;

namespace Engine.Rendering
{
    /// <summary>
    ///     Device wrapper, not yet used
    /// </summary>
    public class Renderer
    {
        private Device _device;
        private Control _window;

        private ArrayList _renderables;
        
        public Renderer(Control window, PresentParameters param)
        {
            // Initiate attributes
            _window = window;
            _renderables = new ArrayList();

            // Initiate device
            _device = new Device(0, DeviceType.Hardware, _window, CreateFlags.HardwareVertexProcessing, param);
            _device.Transform.Projection = Matrix.PerspectiveFovLH((float)System.Math.PI / 4, _window.Width / _window.Height, 1f, 350f);
            _device.RenderState.Lighting = true;

            // Create listener
            _device.DeviceReset += new EventHandler(this.OnDeviceReset);
        }

        // Functions
        public void AddRenderable(IRenderable r)
        {
            if (! _renderables.Contains(r))
            {
                // Initiate renderable
                r.Init(_device);

                // Add renderable to collection
                _renderables.Add(r);
            }
        }

        // Events
        public virtual void OnDeviceReset(object sender, EventArgs e)
        {
            // Change device
            _device = sender as Device;

            // Maintain ratio
            _device.Transform.Projection = Matrix.PerspectiveFovLH((float)System.Math.PI / 4, _window.Width / _window.Height, 1f, 350f);
        }

        // Acessors
        public Device Device
        {
            get { return _device; }
        }

        public Control Window
        {
            get { return _window; }
        }
    }
}
